package Service;

import domain.House;

public class HouseService {
    private House[]houses;
    private int houseNums=1;
    private int id=2;
    public HouseService(int size){
        houses=new House[size];
        houses[0]=new House(1,"zc","110","北京",3000,"未出租");
    }
//    public House show(int id){
//        return houses
//    }
    public boolean find(int id){
        if(id>this.id||id<=0){
            return false;
        }else {
            System.out.println(houses[id-1]);
            return true;
        }
    }
    public void sub(int id){
        if(houseNums==0){
            System.out.println("无法删除");
        }else if (id==this.id){
            houses[id-1]=null;
            System.out.println("删除成功");
            this.houseNums--;
            this.id--;
        }else if(id<this.id){
            for (int i = id; i <this.id; i++) {
                houses[i-1]=houses[i];
            }
            this.id--;
            this.houseNums--;
            houses[this.id-1]=null;
            //改编号
            for (int i = id-1; i <houseNums; i++) {
                houses[i].setId(getId()-1);
            }
        }
    }
    public boolean add(House newHouse){
        if(houseNums==houses.length){
            System.out.println("数组已满");
            return false;
        }
        houses[houseNums]=newHouse;
        this.houseNums++;
        this.id++;
        return true;
    }
    public House[] list(){
        return houses;
    }
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public House[] getHouses() {
        return houses;
    }
}
